<?php
/**
 * VirtualController notify real modules of administrative actions
 * and give them a chance to respond.
 * 
 * @uses Controller
 * @version $id$
 * @copyright 2010 &copy; The YAP Group
 * @author Flavius Aspra <flav@yet-another-project.com> 
 * @license License {@link http://yet-another-project.com/project/yap/LICENSE.txt}
 */
class VirtualController extends Controller {
	public function actionNotify() {
		$route = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());
		try {
			//try default behavior
			Yii::app()->runController($route);
		}
		catch(Exception $e) {
			//try to run it "virtually"
			//@todo do it cleanly (?)
			$parts = explode('/',$route);
			if($parts[0] !== $this->module->id) {
				throw $e;
			}
			if(!in_array($parts[1],array_keys(Yii::app()->getModules()))) {
				throw new CHttpException('Module '.$parts[1].' does not exist',404);
			}
			$ev = new ExtEvent($this,FALSE);
			$mh = Yii::app()->modulehook;
			$hooks = $mh->getHooks('modules.admin.controllers.VirtualController.'.$parts[2],array($parts[1]));
			$args = array(
				'event' => $ev
			);
			if(count($hooks)) {
				foreach($hooks as $k => $v) {
					$mh->runHook($v,$args);
				}
			}
			else {
				$ev->data = 'No hook to handle it.';
			}
			$this->render('notify',array('route' => $route,'event' => $ev));
		}
	}
}
